﻿/*
 * @Autor: PK
 * @Date: 2021/12/29 19:58:SS
 */

using System;
using System.Collections.Generic;

namespace ShkSimulation.core.dao.statistic.feature {

	public class VariableAndHourStatisticKey : IComparable<VariableAndHourStatisticKey> {

		public readonly static string EmptyHourStatisticName = "最新值";

		public string VariableName { get; init; }
		
		public string HourStatisticName { get; init; }

		public VariableAndHourStatisticKey(string variableName, string hourStatisticName = null) {
			VariableName = variableName;
			HourStatisticName = hourStatisticName ?? EmptyHourStatisticName;
		}

		#region 默认

		public void Deconstruct(out string variableName, out string hourStatisticName) {
			variableName = VariableName;
			hourStatisticName = HourStatisticName;
		}
		
		private sealed class VariableNameHourStatisticNameRelationalComparer : IComparer<VariableAndHourStatisticKey> {

			public int Compare(VariableAndHourStatisticKey x, VariableAndHourStatisticKey y) {
				if (ReferenceEquals(x, y)) return 0;
				if (ReferenceEquals(null, y)) return 1;
				if (ReferenceEquals(null, x)) return -1;
				var variableNameComparison = string.Compare(x.VariableName, y.VariableName, StringComparison.Ordinal);
				if (variableNameComparison != 0) return variableNameComparison;
				return string.Compare(x.HourStatisticName, y.HourStatisticName, StringComparison.Ordinal);
			}

		}

		public static IComparer<VariableAndHourStatisticKey> VariableNameHourStatisticNameComparer { get; } = new VariableNameHourStatisticNameRelationalComparer();

		public int CompareTo(VariableAndHourStatisticKey other) {
			if (ReferenceEquals(this, other)) return 0;
			if (ReferenceEquals(null, other)) return 1;
			var variableNameComparison = string.Compare(VariableName, other.VariableName, StringComparison.Ordinal);
			if (variableNameComparison != 0) return variableNameComparison;
			return string.Compare(HourStatisticName, other.HourStatisticName, StringComparison.Ordinal);
		}

		protected bool Equals(VariableAndHourStatisticKey other) {
			return VariableName == other.VariableName && HourStatisticName == other.HourStatisticName;
		}

		public override bool Equals(object obj) {
			if (ReferenceEquals(null, obj)) return false;
			if (ReferenceEquals(this, obj)) return true;
			if (obj.GetType() != this.GetType()) return false;
			return Equals((VariableAndHourStatisticKey) obj);
		}

		public override int GetHashCode() {
			return HashCode.Combine(VariableName, HourStatisticName);
		}

		public override string ToString() {
			if (VariableAndHourStatisticKey.EmptyHourStatisticName.Equals(HourStatisticName)) {
				return VariableName;
			}
			return $"{VariableName}-{HourStatisticName}";
		}
		
		#endregion
	}

}